package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbDeployNodeDao;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbDeployNodeEnum;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbDeployNode;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.service.inf.IDeployNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-10-23 11:17
 **/

@Service
public class DeployNodeService implements IDeployNodeService {
    @Autowired
    TbDeployNodeDao tbDeployNodeDao;

    @Override
    public int save(TbDeployNode record) {
        return tbDeployNodeDao.insertSelective(record);
    }

    @Override
    public int update(TbDeployNode record) {
        return tbDeployNodeDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbDeployNode get(Long id) {
        TbDeployNode entity = tbDeployNodeDao.selectByPrimaryKey(id);
        if (entity != null && TbPublicEnum.GMD_IS_DEL.NO.code.equals(entity.getGmtIsDeleted())) {
            return entity;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        TbDeployNode item = new TbDeployNode();
        item.setId(id);
        item.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.YES.code);
        return update(item);
    }

    @Override
    public List<TbDeployNode> getList(TbDeployNode where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbDeployNodeDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbDeployNode where, TbCustomWhere customWhere) {
        return tbDeployNodeDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbDeployNode selectOne(TbDeployNode where, TbCustomWhere customWhere) {
        List<TbDeployNode> itemList = tbDeployNodeDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public int insertList(List<TbDeployNode> recordList) {
        return tbDeployNodeDao.insertList(recordList);
    }

    @Override
    public int updateHealthState(Long deployTaskId, String host, TbDeployNodeEnum.HEALTH_STATE healthState) {
        TbDeployNode where = new TbDeployNode();
        where.setDeployTaskId(deployTaskId);
        where.setHost(host);
        //
        TbDeployNode record = new TbDeployNode();
        record.setHealthState(healthState.code);
        return tbDeployNodeDao.updateWithDynamicWhere(record, where);
    }
}
